home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group98a.txt / 000129_icon-group-sender _Fri Mar 13 12:35:03 1998.msg < prev    next >
Internet Message Format  |  2000-09-20  |  4KB

  1. Return-Path: <icon-group-sender>
  2. Received: from kingfisher.CS.Arizona.EDU (kingfisher.CS.Arizona.EDU [192.12.69.239])
  3.     by baskerville.CS.Arizona.EDU (8.8.7/8.8.7) with SMTP id MAA14385
  4.     for <icon-group-addresses@baskerville.CS.Arizona.EDU>; Fri, 13 Mar 1998 12:35:03 -0700 (MST)
  5. Received: by kingfisher.CS.Arizona.EDU (5.65v4.0/1.1.8.2/08Nov94-0446PM)
  6.     id AA15705; Fri, 13 Mar 1998 12:35:02 -0700
  7. Date: Fri, 13 Mar 1998 08:38:12 -0700
  8. From: swampler@noao.edu (Steve Wampler)
  9. Subject: Re: Letter Probabilities
  10. To: icon-group@optima.CS.Arizona.EDU
  11. Message-Id: <swampler-9802131538.AA00545533@orpheus.gemini.edu>
  12. In-Reply-To: <35089F74.6018@gte.net>
  13. Errors-To: icon-group-errors@optima.CS.Arizona.EDU
  14. Status: RO
  15. Content-Length: 2820
  16.  
  17.  
  18.  
  19. Mark Evans wrote:
  20. > Here is a small Icon problem related to letter probabilites.  Each
  21. > letter has a "probability of occurrence" in the information-theoretic
  22. > setting.  This probability can be estimated from sample texts.  I want
  23. > to generate random text based on these probabilities.
  24. > I have a table that associates inidividual letters (one-char strings)
  25. > with real numbers (probabilities).  We can assume for the sake of
  26. > argument that the sum of all probabilities in my table is unity.
  27. > Given this table (which I already have Icon code to obtain) what is the
  28. > most efficient method of generating random text?  What I am thinking of
  29. > at the moment is:
  30. > (1) get a sorted list of [key,value] pairs,
  31. >        sorted by value (probability),
  32. >        highest probability first
  33. > (2) generate a random number from 0.0 to 1.0
  34. > (3) use a while-loop to find the slot in the
  35. >        sorted list where the number falls;
  36. >        I would subtract each passing probability
  37. >        until my placeholder value had vanished; e.g.
  38. >        i := 0   # running index
  39. >        x := ?0  # random number 0.0 - 1.0
  40. >        while x > 0 do
  41. >        {
  42. >           i +:= 1
  43. >           x -:= prob_list[i][2]
  44. >        }
  45. >        letter := prob_list[i][1]
  46. > This all seems rather awkward to me, especially step (3).  Isn't there
  47. > some construct in Icon that could do this more elegantly?  Some way to
  48. > search a list for a pair of elements that bracket a variable value?
  49.  
  50. Hmmm, one *very fast* to produce random text is to build a string from
  51. the characters in your table, where the probability of each character
  52. controls the number of repetitions of that character.  Then outputing
  53. n characters of random text is:
  54.  
  55.    every writes(|?s \ n)
  56.  
  57. Also, you can even simplify the creation of the string by skipping the
  58. building of the table of probabilities.  (I realize that you already have
  59. code to produce the table, but this is fun to think about anyway.)
  60.  
  61. Here is a complete program that reads in a sample text (including
  62. newline characters) and outputs random text based on the probability
  63. of character occurrence in the sample text:
  64.  
  65. The sample text is assumed to be 10,000,000 characters or fewer
  66. (just for fun - you would probably better off with a more general
  67. approach to reading in the text to remove this arbitrary limit....)
  68.  
  69. I did it this way to keep the solution small...the setting of &random
  70. could be improved, also.
  71.  
  72. ====================
  73. procedure main(args)
  74.  
  75.     limit := integer(\args[1]) | 10000
  76.     &random := map("HhMmSs","Hh:Mm:Ss", &clock)
  77.  
  78.     s := read(,1000000)
  79.     every writes(|?s \ limit)
  80.  
  81. end
  82. =====================
  83.  
  84. --
  85. Steve Wampler - swampler@gemini.edu [Gemini 8m Telescopes Project (under AURA)]
  86. The gods that smiled at your birth are now laughing openly. (Fortune Cookie)
  87.